package com.luxsoft.siipap.em.replica.importadores.cxc.parches;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.HibernateException;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.luxsoft.siipap.em.replica.importadores.ImportadorSupport;
import com.luxsoft.siipap.model.core.ClienteCredito;
import com.luxsoft.siipap.service.ServiceLocator2;
import com.luxsoft.siipap.util.DBUtils;
import com.luxsoft.siipap.ventas.model.Cobrador;
import com.luxsoft.siipap.ventas.model.Vendedor;

/**
 *Actualiza el cobrador y vendedor de los clientes credito
 * 
 * @author Ruben Cancino Ramos
 *
 */
public class AjusteDeCobradoresVendedores {
	
	public void execute(){
		final List<Map<String, Object>> rows=ServiceLocator2
			.getAnalisisJdbcTemplate().queryForList("select b.clave,a.cobrador ,b.VENDEDOR from sw_clientes_credito a join sw_clientes b on (a.cliente_id=b.cliente_id)");
		
		ServiceLocator2.getHibernateTemplate().execute(new HibernateCallback(){
			
			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				ScrollableResults rs=session.createQuery("from ClienteCredito ").scroll();
				while(rs.next()){
					final ClienteCredito credito=(ClienteCredito)rs.get()[0];
					System.out.println("Procesando client: "+ToStringBuilder.reflectionToString(credito));
					//Localizando cliente
					if(credito.getCliente()==null){
						session.delete(credito);
						session.flush();
						continue;
					}
					Object found=CollectionUtils.find(rows, new Predicate(){
						public boolean evaluate(Object input) {
							Map<String, Object> row=(Map<String, Object>)input;
							String clave=(String)row.get("CLAVE");
							//if(clave!=null && (credito.getCliente()!=null))
								//return clave.equals(credito.getCliente().getClave());
							//return false;
							return clave.equals(credito.getClave());
						}						
					});
					if(found!=null){
						
						Map<String,Object> row=(Map<String, Object>)found;
						
						Number cobrador=(Number)row.get("COBRADOR");
						if(cobrador!=null){
							Cobrador c=ImportadorSupport.getInstance().buscarCobrador(cobrador.intValue());
							credito.getCliente().setCobrador(c);
						}
						
						Number vendedor=(Number)row.get("VENDEDOR");
						if(vendedor!=null){
							Vendedor v=ImportadorSupport.getInstance().buscarVendedor(vendedor.intValue());
							credito.getCliente().setVendedor(v);
						}
						
					}else{
						System.out.println("No existe registro para cliente:  "+credito.getClave());
					}
				}
				return null;
			}			
		});
			
	}
	
	public static void main(String[] args) {
		DBUtils.whereWeAre();
		new AjusteDeCobradoresVendedores().execute();
	}

}
